import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

/**
 * Created with IntelliJ IDEA.
 * User: OzZiE
 * Date: 10/31/13
 * Time: 5:58 PM
 * To change this template use File | Settings | File Templates.
 */
public class ShortAStock extends JPanel
{
    //GUI components for top left panel
    private JLabel makeLabel= new JLabel("How much do you want to make on the short?");
    public JTextField makeField = new JTextField();
    private JLabel ownLabel = new JLabel("How many stocks are you purchasing/own?");
    public  JTextField ownField = new JTextField();
    private JButton generate = new JButton("Generate");

    //GUI components for top right panel
    private JLabel stocksLabel = new JLabel("How many stocks are you purchasing/own?");
    public JTextField stocksField = new JTextField();
    private JLabel declineLabel = new JLabel("How much are you anticipating the decline to be?");
    public JTextField declineField= new JTextField();
    private JButton generate2 = new JButton("Generate");

    //main panels declarations
    private JPanel topMainPanel = new JPanel(new BorderLayout());
    private JPanel bottomMainPanel = new JPanel(new BorderLayout());
    private JButton clear = new JButton("Clear");
    private JPanel bottomToBottom = new JPanel(new FlowLayout());
    private JPanel filler = new JPanel();
    private JPanel leftTop = new JPanel(new GridBagLayout());
    private JPanel rightTop = new JPanel(new GridBagLayout());

    //gridbag constraint and insets
    GridBagConstraints c = new GridBagConstraints();
    Insets m = new Insets(2,10,5,5);

    public ShortAStock()
    {
        setLayout(new GridLayout(2, 0));
        leftTop.setBorder(BorderFactory.createTitledBorder("Option 1"));
        rightTop.setBorder(BorderFactory.createTitledBorder("Option 2"));

        /*
            From here until next comment is
            a bunch of action listeners. I did this
            instead of making a whole separate class
            with a bunch of if statements. Easier
            maintenance this way.
         */
        generate.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try{
                double make;
                double stocks;
                double decline;
                double formatted;

                make = Double.parseDouble(makeField.getText());
                stocks = Double.parseDouble(ownField.getText());
                decline = make/stocks;

                DecimalFormat threeDigits = new DecimalFormat("###0.000");
                formatted = Double.valueOf(threeDigits.format(decline));

                if(formatted>0)
                    JOptionPane.showMessageDialog(getRootPane(), "You need a decline of $" +formatted+ " to make your goal.", "Profit", JOptionPane.PLAIN_MESSAGE);
                else
                    JOptionPane.showMessageDialog(getRootPane(), "I had no idea negative numbers equaled a profit!", "Negative Numbers", JOptionPane.INFORMATION_MESSAGE);

                }catch (Exception a){
                    JOptionPane.showMessageDialog(getRootPane(), "You must properly fill out the text fields", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        generate2.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                try{
                double stocks;
                double decline;
                double anticipate;
                double formatted;

                stocks = Double.parseDouble(stocksField.getText());
                decline = Double.parseDouble(declineField.getText());
                anticipate = stocks*decline;

                DecimalFormat threeDigits = new DecimalFormat("###0.000");
                formatted = Double.valueOf(threeDigits.format(anticipate));

                if(formatted>0)
                    JOptionPane.showMessageDialog(getRootPane(), "Your profit will be $" +formatted+ " not including commission fees.", "Profit", JOptionPane.PLAIN_MESSAGE);
                else
                    JOptionPane.showMessageDialog(getRootPane(), "I had no idea negative numbers equaled a profit!", "Negative Numbers", JOptionPane.INFORMATION_MESSAGE);

                }catch (Exception a){
                    JOptionPane.showMessageDialog(getRootPane(), "You must properly fill out the text fields", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        clear.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                makeField.setText(null);
                ownField.setText(null);
                stocksField.setText(null);
                declineField.setText(null);
            }
        });
    }

    //create shortAStock panel
    public void createPanel()
    {
        //leftTop panel setup
        c.gridx = 0;
        c.gridy = 0;
        c.insets = m;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(makeLabel, c);

        c.gridx = 0;
        c.gridy = 1;
        c.ipadx = 100;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(makeField, c);

        c.gridx = 0;
        c.gridy = 2;
        c.ipadx = 0;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(ownLabel, c);

        c.gridx = 0;
        c.gridy = 3;
        c.ipadx = 100;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(ownField, c);

        c.gridx = 0;
        c.gridy = 4;
        c.ipadx = 0;
        c.anchor = GridBagConstraints.WEST;
        leftTop.add(generate, c);

        topMainPanel.add(filler, BorderLayout.NORTH);
        topMainPanel.add(leftTop, BorderLayout.WEST);


        //rightTop panel setup
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.EAST;
        rightTop.add(stocksLabel, c);

        c.gridx = 0;
        c.gridy = 1;
        c.ipadx = 100;
        c.anchor = GridBagConstraints.EAST;
        rightTop.add(stocksField, c);

        c.gridx = 0;
        c.gridy = 2;
        c.ipadx = 0;
        c.anchor = GridBagConstraints.EAST;
        rightTop.add(declineLabel, c);

        c.gridx = 0;
        c.gridy = 3;
        c.ipadx = 100;
        c.anchor = GridBagConstraints.EAST;
        rightTop.add(declineField, c);

        c.gridx = 0;
        c.gridy = 4;
        c.ipadx = 0;
        c.anchor = GridBagConstraints.EAST;
        rightTop.add(generate2, c);

        topMainPanel.add(rightTop, BorderLayout.EAST);

        bottomToBottom.add(clear);
        bottomMainPanel.add(bottomToBottom, BorderLayout.NORTH);

        add(topMainPanel);
        add(bottomMainPanel);
    }
}
